package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.atguigu.yygu.common.resulit.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 查询所有医院设置信息
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result findAllHospitalSet() {
        List<HospitalSet> hospitalSetList = hospitalSetService.list();
        return Result.ok(hospitalSetList);
    }

    /**
     * 删除医院设置
     *
     * @param id 医院id
     * @return
     */
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id) {
        return hospitalSetService.removeById(id) ? Result.ok() : Result.fail();
    }

    /**
     * 条件查询
     *
     * @param current     当前页
     * @param limit       每页记录数
     * @param hospitalSet 查询条件
     */
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSet) {
        Page<HospitalSet> hospitalSetPage = new Page<>(current, limit);
        Page<HospitalSet> page = hospitalSetService.page(hospitalSetPage,
                new LambdaQueryWrapper<HospitalSet>()
                        .like(StringUtils.isNotBlank(hospitalSet.getHosname()), HospitalSet::getHosname, hospitalSet.getHosname())
                        .like(StringUtils.isNotBlank(hospitalSet.getHoscode()), HospitalSet::getHoscode, hospitalSet.getHoscode())
        );
        return Result.ok(page);
    }

    /**
     * 添加医院设置
     */
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态
        hospitalSet.setStatus(1);
        //设置密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(100)));

        return Result.ok(hospitalSetService.save(hospitalSet));
    }

    /**
     * 根据id获取医院设置
     */
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id) {
        return Result.ok(hospitalSetService.getById(id));
    }

    /**
     * 修改医院设置
     */
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        return Result.ok(hospitalSetService.updateById(hospitalSet));
    }

    /**
     * 批量删除医院设置
     */
    @DeleteMapping("/batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> ids) {
        System.out.println(ids);
        return Result.ok(hospitalSetService.removeByIds(ids));
    }

    /**
     * 医院设置解锁和锁定
     */
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        HospitalSet byId = hospitalSetService.getById(id);
        byId.setStatus(status ^ 1);
        return Result.ok(hospitalSetService.updateById(byId));
    }

    /**
     * 发送签名密钥
     */
  /*  @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }*/
}
